﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ET.Score.SharedLibrary
{
  public   class CopyValuesFromParent<T>
    {
        public static T CopyValue(Object o)
        {
            T copyObject = System.Activator.CreateInstance<T>();

            Type type = o.GetType();
            if (type != null)
            {
                UpdateForType(type, o, copyObject);
                type = type.BaseType;
            }
            return copyObject;
        }


        private static void UpdateForType(Type type, Object source, T destination)
        {
            FieldInfo[] myObjectFields = type.GetFields(
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            Type type1 = typeof(T);
            FieldInfo[] myObjectFields1 = type1.GetFields(
              BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo fi in myObjectFields)
            {
         //       if (myObjectFields1.FirstOrDefault(m=>m.Name==fi.Name)!=null)
                   fi.SetValue(destination, fi.GetValue(source));
            }
        }
       public static void CopyValueToAntherObj(T sourceobj,ref T descobj)
        {
            Type type = sourceobj.GetType();
            if (type != null)
            {
                UpdateForType(type, sourceobj, descobj);
                type = type.BaseType;
            }
           
        }
    }
}
